//
//  AddHongbaoVC1.m
//  SelfDriving
//
//  Created by apple on 16/7/1.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AddHongbaoVC1.h"
#import "BusinessService+User.h"
@interface AddHongbaoVC1 ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *money;
@property (weak, nonatomic) IBOutlet UITextField *titlefield;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet UILabel *balanceLbl;

@end

@implementation AddHongbaoVC1

- (void)viewDidLoad {
    [super viewDidLoad];
    _submitBtn.layer.cornerRadius = 4;
    _submitBtn.clipsToBounds = YES;
    //xhw2018-3-15加
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [[AppDelegate shareDelegate]refuser:[AppDelegate shareDelegate].user.uid success:^(id as) {
        LoginResponseModel *user = as;
        _balanceLbl.text = [NSString stringWithFormat:@"当前余额:￥%@",user.money];
    }];
    
    
    //xhw12月26号加:红包金额只能输入数字和一位小数点
    _money.delegate = self;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma  mark 塞入红包
- (IBAction)submitAction:(UIButton *)sender {
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    if ([_money.text trim].length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入金额"];
        return;
    }
    if ([[self.money.text trim] floatValue] <= 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入合法金额！"];
        return;
    }
    [dic setObject:[_titlefield.text trim].length==0?@"恭喜发财，大吉大利！":[_titlefield.text trim] forKey:@"title"];
    [dic setObject:@"1" forKey:@"nums"];
    [dic setObject:_uid forKey:@"fid"];
    [dic setObject:@"2" forKey:@"type"];
    [dic setObject:[_money.text trim] forKey:@"singlemoney"];
    [[BusinessService shareInstance]publish_hongbao:dic success:^(id result) {
        LoginResponseModel *user = result;
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[user.data objectForKey:@"display"] forKey:@"display"];
        [dic setObject:[user.data objectForKey:@"headsmall"] forKey:@"headsmall"];
        [dic setObject:[user.data objectForKey:@"id"] forKey:@"id"];
        [dic setObject:[user.data objectForKey:@"nickname"] forKey:@"nickname"];
        [dic setObject:[user.data objectForKey:@"qiang_money"] forKey:@"qiang_money"];
        [dic setObject:[user.data objectForKey:@"tip"] forKey:@"tip"];
        [dic setObject:[user.data objectForKey:@"title"] forKey:@"title"];
        [dic setObject:[user.data objectForKey:@"type"] forKey:@"type"];
        [_degetage didSelectHongbao:dic];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
}
- (IBAction)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length > 5) {
        return range.location < 6;
    }
    else if (textField.text.length == 5) {
        
        NSString *newStr = [textField.text stringByAppendingString:string];
        if ([newStr floatValue] > 100000) {
            [textField.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }
        else {
            BOOL isHaveDian = YES;
            if ([textField.text rangeOfString:@"."].location==NSNotFound) {
                isHaveDian=NO;
            }
            if ([string length]>0) {
                unichar single=[string characterAtIndex:0];//当前输入的字符
                if (single=='.')
                {
                    if(!isHaveDian)//text中还没有小数点
                    {
                        isHaveDian=YES;
                        return YES;
                    }else
                    {
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                    }
                }
                else
                {
                    if (isHaveDian)//存在小数点
                    {
                        //判断小数点的位数
                        NSRange ran=[textField.text rangeOfString:@"."];
                        NSInteger tt=range.location-ran.location;
                        if (tt <= 2){
                            return YES;
                        }else{
                            return NO;
                        }
                    }
                    else
                    {
                        return YES;
                    }
                }
            }
            else
            {
                return YES;
            }
        }
    }
    else{
        BOOL isHaveDian = YES;
        if ([textField.text rangeOfString:@"."].location==NSNotFound) {
            isHaveDian=NO;
        }
        if ([string length]>0)
        {
            unichar single=[string characterAtIndex:0];//当前输入的字符
            
            if ((single >='0' && single<='9') || single=='.')//数据格式正确
            {
                //首字母不能为小数点
                if([textField.text length]==0){
                    if(single == '.'){
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                        
                    }
                }
                if([textField.text length]==1 && [textField.text isEqualToString:@"0"]){
                    if(single != '.'){
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                        
                    }
                }
                if (single=='.')
                {
                    if(!isHaveDian)//text中还没有小数点
                    {
                        isHaveDian=YES;
                        return YES;
                    }else
                    {
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                    }
                }
                else
                {
                    if (isHaveDian)//存在小数点
                    {
                        //判断小数点的位数
                        NSRange ran=[textField.text rangeOfString:@"."];
                        NSInteger tt=range.location-ran.location;
                        if (tt <= 2){
                            return YES;
                        }else{
                            return NO;
                        }
                    }
                    else
                    {
                        return YES;
                    }
                }
            }else{//输入的数据格式不正确
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                return NO;
            }
        }
        else
        {
            return YES;
        }
    }
}

@end
